package com.mybed.crm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybed.crm.common.enums.HttpCodeEnum;
import com.mybed.crm.pojo.dto.PageContractQueryDto;
import com.mybed.crm.pojo.pojo.*;
import com.mybed.crm.pojo.vo.ContractVo;
import com.mybed.crm.result.PageResult;
import com.mybed.crm.result.Result;
import com.mybed.crm.service.*;
import com.mybed.crm.utils.thread.UserThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 合同 前端控制器
 * </p>
 *
 * @author kt
 * @since 2024-10-26
 */
@RestController
@RequestMapping("/contract")
@Slf4j
@Api(tags = "合同管理")
public class ContractController {
    @Autowired
    private IContractService contractService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;
    @Autowired
    private BusinessService businessService;

    @GetMapping("/list")
    @ApiModelProperty(value = "合同列表")
    public PageResult<ContractVo> pageContractQuery(PageContractQueryDto pageContractQueryDto) {
        return contractService.pageContractQuery(pageContractQueryDto);
    }

    @ApiModelProperty(value = "根据id查询合同")
    @GetMapping("/{id}")
    public Result selectContractId(@PathVariable Long id) {
        return Result.success(contractService.getById(id));
    }

    @ApiModelProperty(value = "添加合同")
    @PostMapping()
    public Result addContract(@RequestBody Contract contract) {
        extracted(contract);

        contract.setCreateTime(LocalDateTime.now());
        return Result.success(contractService.save(contract));


    }

    // 添加合同时，需要回显活动名称，课程名称，课程价格，合同金额，合同折扣类型
    private void extracted(Contract contract) {
        //活动名称
        Long activityId = contract.getActivityId();
        contract.setActivityName(activityService.getById(activityId).getName());
        contract.setDiscountType(String.valueOf(activityService.getById(activityId).getType()));
        //课程名称
        Long courseId = contract.getCourseId();
        contract.setCourseName(courseService.getById(courseId).getName());
        contract.setContractOrder(courseService.getById(courseId).getPrice().floatValue());
        contract.setCoursePrice(courseService.getById(courseId).getPrice().floatValue());
        //创建人id
        String userId = UserThreadLocalUtil.getUser();
        User user = userService.getById(userId);
        contract.setCreateBy(user.getUserName());
        contract.setDeptId(Long.valueOf(user.getDeptId()));
    }

    @ApiModelProperty(value = "修改合同")
    @PutMapping()
    public Result updateContract(@RequestBody Contract contract) {
        extracted(contract);
        return Result.success(contractService.updateById(contract));
    }

    @ApiModelProperty(value = "根据id回显合同")
    @GetMapping("/detail/{id}")
    public Result selectContractsId(@PathVariable Long id) {
        return Result.success(contractService.getById(id));
    }


    /**
     * 商机转为客户
     * @param id
     * @param contract
     * @return
     */
    @PutMapping("/changeContract/{id}")
    public Result addContracts(@PathVariable Long id, @RequestBody Contract contract){
        contractService.addContracts(id,contract);
        return Result.success();
    }

    /*@PutMapping("/changeContract/{id}")
    @ApiModelProperty(value = "商机转合同")
    private Result changeContract(@PathVariable Long id,String contractNo,String fileName,String phone) {
        Business business = businessService.getById(id);
        Contract contract = new Contract();
        contract.setBusinessId(id);
        contract.setCourseId(Long.valueOf(business.getCourseId()));
        contract.setActivityId(Long.valueOf(business.getActivityId()));
//      contract.setContractOrder(Float.valueOf(business.getSalary()));
//      contract.setCoursePrice(Float.valueOf(business.getSalary()));
        contract.setDiscountType(String.valueOf(business.getLevel()));
        contract.setActivityName(business.getActivityName());
        contract.setCourseName(business.getSubject());
        contract.setName(business.getName());
        contract.setPhone(phone);
        contract.setSubject(business.getSubject());
        contract.setChannel(String.valueOf(business.getChannel()));
        contract.setStatus(String.valueOf(business.getStatus()));
        contract.setCreateTime(LocalDateTime.now());
        contract.setContractNo(contractNo);
        contract.setFileName(fileName);
        //创建人id
        String userId = UserThreadLocalUtil.getUser();
        User user = userService.getById(userId);
        contract.setCreateBy(user.getUserName());
        contract.setDeptId(Long.valueOf(user.getDeptId()));
        return Result.success(contractService.save(contract));
    }*/
}
